<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/12/012
 * Time: 9:42
 */

namespace app\api\service;

use app\api\model\PatternMaster as PatternMasterModel;
use app\lib\exception\OrderException;
use app\lib\exception\OrderFigureMeasureDataException;
use app\lib\exception\PatternMasterException;
use app\api\model\Order as OrderModel;
use app\api\model\OrderFigureMeasureData as OrderFigureMeasureDataModel;
use think\Db;
use think\Exception;
use app\api\model\Config as ConfigModel;

class PatternMaster {

    /**
     * @param $id
     * @return bool
     * @throws OrderException
     * @throws PatternMasterException
     * @throws \Exception
     */
    public static function orderReceiving($id) {

        $pattern_master = PatternMasterModel::getPatternMasterInfo();
        if (!$pattern_master) {
            throw new PatternMasterException();
        }

        $order = OrderModel::get(['id' => $id]);
        if (!$order) {
            throw new OrderException();
        }

        if ($order['status'] != 2 ) {
            throw new OrderException([
                'msg' => '订单状态错误',
                'errorCode' => 60002
            ]);
        }

        if ($order['pattern_master_id'] != 0 ) {
            throw new PatternMasterException([
                'msg' => '手慢了，订单已被接走',
                'errorCode' => 13003
            ]);
        }

        Db::startTrans();
        try {
            // 用户关联量体师ID为空  则进行关联
            $member = $order->orderMember;
            if ($member->relevance_pattern_master_id == 0) {
                $member->save(['relevance_pattern_master_id' => $pattern_master['id']]);
            }
            // 更新订单数据
            $order->save(['status' => 3, 'pattern_master_id' => $pattern_master['id'], 'pattern_master_receive_time' => time()]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }

        return true;
    }

    /**
     * 确认量体完成
     * @param $id
     * @throws OrderException
     * @throws OrderFigureMeasureDataException
     * @throws PatternMasterException
     */
    public static function affirmMeasure($id) {

        $pattern_master = PatternMasterModel::getPatternMasterInfo();
        if ($pattern_master) {
            throw new PatternMasterException();
        }

        $order = OrderModel::get(['id' =>$id, 'pattern_master_id' => $pattern_master['id']]);
        if (!$order) {
            throw new OrderException();
        }

        if ($order['status'] != 3 ) {
            throw new OrderException([
                'msg' => '订单状态错误',
                'errorCode' => 60002
            ]);
        }

        // 获取订单量体数据
        $order_figure_measure_data = OrderFigureMeasureDataModel::get(['order_id' => $order['id']]);
        if (!$order_figure_measure_data) {
            throw new OrderFigureMeasureDataException();
        }

        // 更新订单数据
        $order->save(['status' => 4,  'pattern_master_affirm_time' => time()]);

        $part_order_number = ConfigModel::get(['name' => 'order_inform_number']);
        $manage_mobile = $part_order_number['value'];

        // 短信通知
        $param = [
            'mobile' => $manage_mobile,
            'order_sn' => $order['order_sn'],
            'type' => 4,
        ];
        NoteCode::send_code($param);
    }
}